package com.graduation.mybatis.controller;

import com.graduation.mybatis.bean.News;
import com.graduation.mybatis.bean.NewsDTO;
import com.graduation.mybatis.bean.NewsListDTO;
import com.graduation.mybatis.service.NewsService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (News)表控制层
 *
 * @author makejava
 * @since 2025-05-01 16:18:21
 */
@RestController
@RequestMapping("news")
public class NewsController {
    /**
     * 服务对象
     */
    @Resource
    private NewsService newsService;

    /**
     * 分页查询
     *
     * @param news 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
//    @GetMapping
//    public ResponseEntity<Page<News>> queryByPage(News news, PageRequest pageRequest) {
//        return ResponseEntity.ok(this.newsService.queryByPage(news, pageRequest));
//    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<News> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.newsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param news 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<News> add(News news) {
        return newsService.save(news) ?
                ResponseEntity.ok(news) :
                ResponseEntity.internalServerError().build();
    }

    /**
     * 编辑数据
     *
     * @param news 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<News> edit(News news) {
        return newsService.updateById(news) ?
                ResponseEntity.ok(news) :
                ResponseEntity.internalServerError().build();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.newsService.removeById(id));
    }

    /**
     * 获取新闻详情
     *
     * @param id     新闻ID
     * @param userId 用户ID
     * @return 新闻详情
     */
    @GetMapping("/detail")
    public ResponseEntity<Map<String, Object> > getNewsDetail(@RequestParam Long id, @RequestParam(required = false) Long userId) {
        Map<String, Object> response = new HashMap<>();
        NewsDTO newsDTO = newsService.getNewsDetail(id, userId);
        if(newsDTO != null){
            response.put("message",  "获取新闻详情成功");
            response.put("success", true);
            response.put("data", newsDTO);
        } else {
            response.put("message",  "获取新闻详情错误");
            response.put("success", false);
        }
        return ResponseEntity.ok(response);
    }
    /**
     * 分页获取新闻列表
     *
     * @param page 页码
     * @param pageSize 每页数量
     * @return 新闻列表
     */
    @GetMapping("/list")
    public List<NewsListDTO> getNewsList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        return newsService.getNewsList(page, pageSize);
    }

}

